package com.feeyo.monitor.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.feeyo.monitor.domain.User;
import com.feeyo.monitor.service.ExcelParserService;
import com.feeyo.monitor.service.UserService;
import com.feeyo.monitor.service.UserVehiclePermissionService;
import com.feeyo.monitor.utils.DeptTreeNode;
import org.postgresql.util.PSQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.feeyo.monitor.domain.Department;
import com.feeyo.monitor.service.DepartmentService;
import com.feeyo.monitor.utils.GloabalConstant;
import com.feeyo.monitor.utils.ResultData;

/**
 * 部门类REST接口
 * @Author <a href="mailto:bliulei@variflight.com">liulei</a>
 * @Description Created by liulei on 2017/5/18 下午5:42.
 */
@RestController
@RequestMapping("/manager") 
public class DeptController {
	
	@Autowired
	DepartmentService departmentService;
	@Autowired
    UserVehiclePermissionService userVehiclePermissionService;
    @Autowired
    UserService userService;

	/**
	 * @Description  获取部门列表
	 * @params
	 * @return
	 */
	@RequestMapping(value="/depts", method=RequestMethod.GET) 
	public ResultData<List<Department>> getdepts(@RequestParam(value="deptName",required=false) String deptName,
			                                    @RequestParam(value="code",required=false) String code,
                                                 @RequestParam(value="userId",required=false)String userId){
		ResultData<List<Department>> rd = new ResultData<List<Department>>();
		Map<String,Object> param = new HashMap<String,Object>();
        String parentId = "";

		if (deptName != null)
			param.put("deptName", deptName);
		if (code != null)
			param.put("code", code);

        if (userId != null){
            User users = userService.selectByPrimaryKey(userId);
            if(!users.getAdminis()) {
                String deptId = userVehiclePermissionService.selectDeptByUserId(userId);
                parentId = deptId;
                System.out.println("deptId->" + deptId);
                if (deptId.equals("*"))
                    ;
                else {
                    List<String> deptIds = departmentService.selectTreeNode(deptId);
                    System.out.println("deptIds->" + deptIds.toString());
                    param.put("deptIds", deptIds);
                }
            }
        }

		List<Department> result = null;
		try {
			result = departmentService.selectByPage(param);
		}catch (Exception e){
			rd.setCode(GloabalConstant.FAILED);
			rd.setMessage(e.getMessage());
			e.printStackTrace();
			return rd;
		}
		rd.setCode(GloabalConstant.SUCCESS);
		rd.setData(result);
		return rd;
	}
    /**
     * @Description 获取树状部门列表
     * @params
     * @return JSON格式
     */
    @RequestMapping(value="/depts/tree", method=RequestMethod.GET)
    public ResultData<List<DeptTreeNode>> deptTree(@RequestParam(value="userId",required=false)String userId){
        ResultData<List<DeptTreeNode>> rd = new ResultData<List<DeptTreeNode>>();
        Map<String,Object> param = new HashMap<String,Object>();
        List<DeptTreeNode> result = null;
        String parentId = "";
        if (userId != null){
            User users = userService.selectByPrimaryKey(userId);
            if(!users.getAdminis()) {
                String deptId = userVehiclePermissionService.selectDeptByUserId(userId);
                parentId = deptId;
                System.out.println("deptId->" + deptId);
                if (deptId.equals("*"))
                    ;
                else {
                    List<String> deptIds = departmentService.selectTreeNode(deptId);
                    System.out.println("deptIds->" + deptIds.toString());
                    param.put("deptIds", deptIds);
                }
            }
        }

        try {
            result = departmentService.selectByTree(param,parentId);
        }catch (Exception e){
            rd.setCode(GloabalConstant.FAILED);
            rd.setMessage(e.getMessage());
            e.printStackTrace();
            return rd;
        }
        rd.setCode(GloabalConstant.SUCCESS);
        rd.setData(result);
        return rd;
    }

    /**
     * 获取单个部门
     * @param deptId
     * @return
     */
    @RequestMapping(value="/depts/child/{deptId}", method=RequestMethod.GET)
    public ResultData<List<String>> getDeptChildren(@PathVariable("deptId")String deptId){
        ResultData<List<String>> rd = new ResultData<List<String>>();
        List<String> result = null;
        try {
            result = departmentService.selectTreeNode(deptId);
        } catch (Exception e) {
            rd.setCode(GloabalConstant.FAILED);
            rd.setMessage(e.getMessage());
            e.printStackTrace();
            return rd;
        }
        rd.setCode(GloabalConstant.SUCCESS);
        rd.setData(result);
        return rd;
    }

	/**
	 * 获取单个部门
	 * @param deptId
	 * @return
	 */
	@RequestMapping(value="/depts/{deptId}", method=RequestMethod.GET) 
	public ResultData<Department> getDept(@PathVariable("deptId")String deptId){
		ResultData<Department> rd = new ResultData<Department>();
		Department result = null;
		try {
			result = departmentService.selectByPrimaryKey(deptId);
		} catch (PSQLException e) {
			rd.setCode(GloabalConstant.FAILED);
			rd.setMessage(e.getMessage());
			e.printStackTrace();
			return rd;
		}
		rd.setCode(GloabalConstant.SUCCESS);
		rd.setData(result);
		return rd;
	}

    /**
     * @Description 新建/修改 部门
     * @params
     * @return
     */
	@RequestMapping(value="/depts", method=RequestMethod.POST)
	public ResultData<Object> saveDept(@RequestParam(value="deptId",required=false)String deptId,
									   @RequestParam(value="deptName",required=false)String deptName,
									   @RequestParam(value="parentId",required=false)String parentId,
									   @RequestParam(value="code",required=false)String code){

		ResultData<Object> rd = new ResultData<Object>();
		Department dept = new Department();
		dept.setId(deptId);
		dept.setName(deptName);
		if(parentId !=null){
		    if (parentId.equals(""))
		        dept.setParentid(null);
		    else
		        dept.setParentid(parentId);
        }else
		    dept.setParentid(parentId);
		dept.setCode(code);
		try {
			if (deptId == null)
				departmentService.insert(dept);
			else
				departmentService.updateByPrimaryKeySelective(dept);
		}catch (Exception e){
			rd.setCode(GloabalConstant.FAILED);
			rd.setMessage(e.getMessage());
			e.printStackTrace();
			return rd;
		}
		rd.setCode(GloabalConstant.SUCCESS);
		return rd;
	}


	/**
	 * @Description 删除部门
	 * @params
	 * @return
	 */
	@RequestMapping(value="/depts/delete/{deptId}", method=RequestMethod.GET) 
	public ResultData<Object> delete(@PathVariable("deptId")String deptId){
		ResultData<Object> rd = new ResultData<Object>();
		try{
			departmentService.updateDeleteStatus(deptId);
		}catch(Exception e){
			rd.setCode(GloabalConstant.FAILED);
			rd.setMessage(e.getMessage());
			e.printStackTrace();
			return rd;
		}
		rd.setCode(GloabalConstant.SUCCESS);
		return rd;
	}
}
